package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import android.widget.Toast
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.ToastUtils
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.customview.HisPop
import hlhj.fhp.supreme.javabean.MultiBean
import hlhj.fhp.supreme.javabean.PhoneBean
import hlhj.fhp.supreme.javabean.ReadHisBean
import hlhj.fhp.supreme.javabean.ReadListBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_read_aty.*
import java.util.ArrayList

class ReadAty : BaseAty() {
    private lateinit var multiAdp:BaseMultiItemQuickAdapter<ReadListBean.DataBeanX,BaseViewHolder>
    private val datas=ArrayList<ReadListBean.DataBeanX>()
    private val hisDatas=ArrayList<ReadHisBean.DataBean>()
    private lateinit var year:String
    private lateinit var month:String
    private lateinit var startTimeStamp:String
    private lateinit var enTime:String
    private var des=""
    private lateinit var alreadyAdp:BaseQuickAdapter<ReadHisBean.DataBean,BaseViewHolder>
    override fun getContentId(): Int = R.layout.activity_read_aty

    override fun initView() {
        OkGo.post<PhoneBean>(Urls.BaseUrl+Urls.PHONE)
                .params("type","book_des")
                .execute(object :JsonCallBack<PhoneBean>(this){
                    override fun onSuccess(response: Response<PhoneBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            des=bean?.data.book_des
                        }else{
                            ToastUtils.toast(this@ReadAty,bean?.msg!!)
                        }
                    }
                })
        alreadyAdp=object :BaseQuickAdapter<ReadHisBean.DataBean,BaseViewHolder>(R.layout.read_already_item,hisDatas){
            override fun convert(helper: BaseViewHolder?, item: ReadHisBean.DataBean?) {
                helper?.setText(R.id.itemTittle,item?.title)
                helper?.setText(R.id.itemInfo,item?.desc)
                helper?.setText(R.id.itemProgress,"已读：第${item?.sort}课")
                helper?.setText(R.id.itemTime,MyUtils.timeStampToStr(item?.create_time!!.toLong()))
                if (helper?.adapterPosition==hisDatas.size-1){
                    helper?.setVisible(R.id.dv,false)
                }else{
                    helper?.setVisible(R.id.dv,true)

                }
                Glide.with(mContext).load(item?.img).into(helper?.getView(R.id.itemIcon))
            }
        }
        multiAdp=object :BaseMultiItemQuickAdapter<ReadListBean.DataBeanX,BaseViewHolder>(datas){
            init {
                addItemType(1,R.layout.read_head)
                addItemType(2,R.layout.read_book)
                addItemType(4,R.layout.read_already)//已读书籍
                addItemType(5,R.layout.read_scene)

            }
            override fun convert(helper: BaseViewHolder?, item: ReadListBean.DataBeanX?) {
                when(helper?.itemViewType){
                    1->{
                        helper?.setText(R.id.tv2,des)
                        val btGoVip = helper?.getView<TextView>(R.id.btVip)
                        btGoVip.setOnClickListener {
                            val intent = Intent(this@ReadAty, MyVipAty::class.java)
                            startActivity(intent)
                        }
                    }
                    2->{
                        helper?.setText(R.id.tvTittle,item?.name)

                        val recyclerView = helper?.getView<RecyclerView>(R.id.recyclerview)
                        if (item?.data!=null){
                            val inData = item?.data
                            val adp =object :BaseQuickAdapter<ReadListBean.DataBeanX.DataBean,BaseViewHolder>(R.layout.read_book_item2,inData){
                                override fun convert(helper: BaseViewHolder?, item: ReadListBean.DataBeanX.DataBean?) {
                                    helper?.setText(R.id.itemTittle,item?.title)
                                    Glide.with(mContext).load(item?.img).asBitmap().into(helper?.getView(R.id.itemIcon))

                                }
                            }
                            recyclerView.adapter=adp
                            recyclerView.layoutManager=LinearLayoutManager(mContext,LinearLayout.HORIZONTAL,false)
                            adp.setOnItemClickListener { adapter, view, position ->
                                val intent = Intent(mContext, RedDetailAty::class.java)
                                intent.putExtra("id",inData[position].id)
                                intent.putExtra("money",inData[position].money)
                                mContext.startActivity(intent)
                            }


                        }

                    }
                    4->{//历史读物
                        val recyclerView = helper?.getView<RecyclerView>(R.id.recyclerview)
                        helper?.setText(R.id.tvYear,"选择年")
                        helper?.setText(R.id.tvMonth,"选择月")
                        val tvYear = helper?.getView<TextView>(R.id.tvYear)
                        val tvMonth = helper?.getView<TextView>(R.id.tvMonth)
                        val yearPop = HisPop(mContext, 1,object :HisPop.OnTimeClickListener{
                            override fun onTimeClick(i: Int) {
                                year=i.toString()
                                tvYear.text=year+"年"
                                refreshHis()
                            }
                        })
                        val monthPop = HisPop(mContext, 2,object :HisPop.OnTimeClickListener{
                            override fun onTimeClick(i: Int) {
                                month=i.toString()
                                tvMonth.text=month+"月"
                                refreshHis()
                            }
                        })
                        tvYear.setOnClickListener {
                            if (yearPop.isShowing){
                                yearPop.dismiss()
                            }else{
                                yearPop.showAsDropDown(tvYear)
                            }
                        }
                        tvMonth.setOnClickListener {
                            if (monthPop.isShowing){
                                monthPop.dismiss()
                            }else{
                                monthPop.showAsDropDown(tvMonth)
                            }
                        }
                        recyclerView.adapter=alreadyAdp
                        recyclerView.layoutManager=LinearLayoutManager(this@ReadAty,LinearLayout.VERTICAL,false)

                    }//历史读物
                    5->{//视频读物
                        helper?.setText(R.id.tv1,item?.name)
                        val recyclerView = helper?.getView<RecyclerView>(R.id.recyclerview)
                        if (item?.data!=null){
                            val inData = item?.data
                            val adp =object :BaseQuickAdapter<ReadListBean.DataBeanX.DataBean,BaseViewHolder>(R.layout.read_book_item,inData){
                                override fun convert(helper: BaseViewHolder?, item: ReadListBean.DataBeanX.DataBean?) {
                                    helper?.setText(R.id.itemTittle,item?.title)
                                    Glide.with(mContext).load(item?.img).asBitmap().into(helper?.getView(R.id.itemIcon))
                                    val layout = helper?.getView<RelativeLayout>(R.id.lo)
                                    val params = layout?.layoutParams as GridLayoutManager.LayoutParams
                                    when(helper?.adapterPosition!! %2){
                                        0->{
                                            params.setMargins(0,0,10,0)
                                        }
                                        1->{
                                            params.setMargins(10,0,0,0)
                                        }
                                    }
                                    layout.layoutParams=params

                                }
                            }
                            recyclerView.adapter=adp
                            recyclerView.layoutManager=GridLayoutManager(this@ReadAty,2)
                            adp.setOnItemClickListener { adapter, view, position ->
                                val intent = Intent(mContext, RedDetailAty::class.java)
                                intent.putExtra("id",inData[position].id)
                                intent.putExtra("money",inData[position].money)
                                mContext.startActivity(intent)
                            }
                        }

                    }
                }
            }
        }
        recyclerview.adapter=multiAdp
        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)

        getlist()//获取列表
        timeWork()//第一次获取历史记录
    }

    private fun getlist() {
        OkGo.get<ReadListBean>(Urls.BaseUrl + Urls.READLIST)
                .execute(object : JsonCallBack<ReadListBean>(this) {
                    override fun onSuccess(response: Response<ReadListBean>?) {
                        val bean = response?.body()
                        if (bean?.code == 1) {
                            for (i in 0..bean?.data.size - 1) {
                                if (i == bean?.data.size - 1) {
                                    bean?.data[i].setType(5)
                                } else {
                                    bean?.data[i].setType(2)
                                }
                            }

                            datas.addAll(bean?.data)
                            val head = ReadListBean.DataBeanX()
                            head.setType(1)
                            datas.add(0, head)
                            val his = ReadListBean.DataBeanX()
                            his.setType(4)
                            datas.add(2, his)
                            multiAdp.notifyDataSetChanged()
                        }
                    }
                })
    }

    private fun timeWork() {
        val timeMillis = System.currentTimeMillis()
        val ym = MyUtils.timeStampToYM(timeMillis)
        year = ym.split("-")[0]
        month = ym.split("-")[1]

        var time = "${year}-${month}-01 00:00:00"
        startTimeStamp = MyUtils.dateToStamp(time)
        enTime=(timeMillis/1000).toString()
        LogUtil.logi("现在的时间${startTimeStamp}")
        OkGo.post<ReadHisBean>(Urls.BaseUrl + Urls.READHIS)
                .params("start_time", startTimeStamp)
                .params("end_time",enTime)
                .execute(object :JsonCallBack<ReadHisBean>(this){
                    override fun onSuccess(response: Response<ReadHisBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            if (bean?.data!=null){
                                hisDatas.addAll(bean?.data)
                                alreadyAdp.notifyDataSetChanged()
                            }
                        }
                    }
                })
    }
    private fun refreshHis(){
        var time = "${year}-${month}-01 00:00:00"
        startTimeStamp = MyUtils.dateToStamp(time)
        var endTime="${year}-${month.toInt()+1}-01 00:00:00"
        enTime=MyUtils.dateToStamp(endTime)

        OkGo.post<ReadHisBean>(Urls.BaseUrl + Urls.READHIS)
                .params("start_time", startTimeStamp)
                .params("end_time",enTime)
                .execute(object :JsonCallBack<ReadHisBean>(this){
                    override fun onSuccess(response: Response<ReadHisBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            hisDatas.clear()
                            if (bean?.data==null){

                            }else{
                                hisDatas.addAll(bean?.data)
                            }
                            alreadyAdp.notifyDataSetChanged()
                        }
                    }
                })
    }
    override fun initListener() {
        alreadyAdp.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this, RedDetailAty::class.java)
            intent.putExtra("id",hisDatas[position].rid)
            intent.putExtra("money",hisDatas[position].money)
            this.startActivity(intent)
        }
        btExit.setOnClickListener {
            finish()
        }
    }
}
